/**
 * Title: HTTP Server
 * Description: HTTP Server
 * Copyright: Copyright (c) 2013 Kugou (All rights reserved)
 * Company: Kugou
 * Author: zhiguangq
 * Reviser: <Please input reviser>
 * Date: 2013-9-24
 * Version: 1.0
 * History: [1.0 2013-9-24]
 */

#ifndef HTTPSERVER_HTTP_SERVER_KUGOU_H
#define HTTPSERVER_HTTP_SERVER_KUGOU_H

#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>

#include <kugou/server/http/Handler.h>
#include <kugou/server/http/HTTPConnection.h>
#include <kugou/server/http/HTTPConnectionManager.h>
#include <kugou/server/http/HTTPRequestHandler.h>

namespace kugou
{
namespace server
{
namespace http
{

class HTTPServer
    : private boost::noncopyable
{
public:
    /// Construct the server to listen on the specified TCP address and port, and
    /// server up file from the given directory.
    explicit HTTPServer(const std::string& address, const std::string& port, const std::string& doc_root);

    /// Run the server's io_service loop.
    void run();

    /// Stop the server.
    void stop();

    /// Register Handler
    void registerHandler(kugou::server::http::Handler* handler)
    {
    	m_requestHandler.registerHandler(handler);
    }

private:
    /// Handle completion of an asynchronous accept operation.
    void handleAccept(const boost::system::error_code& e);

    /// Handle a request to stop the server.
    void handleStop();

    /// The io_server used to perform asynchronous operations
    boost::asio::io_service m_ioService;

    /// Acceptor used to listen for incoming connections.
    //boost::asio::ip::tcp::acceptor acceptor_;
    boost::shared_ptr<boost::asio::ip::tcp::acceptor> m_acceptorPtr;

    /// The connection manager which owns all live connections.
    HTTPConnectionManager m_connectionManager;

    /// The next connection to be accepted.
    ConnectionSharePtr m_newConnection;

    /// The handler for all incoming requests.
    HTTPRequestHandler m_requestHandler;

    std::string m_docRoot;
};

} // http    
} // server
} // kugou

#endif
